package com.kuaimacode.kframework.agent.services.holiday;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.agent.util.SessionUtil;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.mybatis.mapper.holiday.HolidayMapper;
import com.kuaimacode.kframework.mybatis.models.holiday.Holiday;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author anzy
 * @date 2019/06/14
 */
@Service
public class HolidayService extends ServiceImpl<HolidayMapper, Holiday> {

    	/**
    	 *  分页查找所有的数据
    	 * @param pageNum 当前页码，从1开始
    	 * @param pageSize  一页显示多少行
    	 * @return
    	 */
    	public PageInfo<Holiday> findAll(int pageNum, int pageSize) {
    		PageHelper.startPage(pageNum, pageSize);
			QueryWrapper queryWrapper = new QueryWrapper();
			queryWrapper.eq("agent_id", SessionUtil.getAgentId());
			queryWrapper.orderByDesc("end_date");
    		List<Holiday> datas = baseMapper.selectList(queryWrapper);
    		return new PageInfo<Holiday>(datas);
    	}

	/**
	 * 查询是否有生效的节假日设置
	 * @param agentId
	 * @param beginDate
	 * @param endDate
	 * @return
	 */
		public List<Holiday> getIsRunning(String agentId,String beginDate,String endDate){

			QueryWrapper<Holiday> queryWrapper = new QueryWrapper();
			queryWrapper.eq("agent_id", agentId);
			queryWrapper.eq("status", Constant.DeleteStateEnum.NORMAL.getCode());
			queryWrapper.le("begin_Date",beginDate);
			queryWrapper.ge("end_date",beginDate);

			List<Holiday> list1= baseMapper.selectList(queryWrapper);
			if(list1!=null&&list1.size()>0) return list1;


			QueryWrapper<Holiday> queryWrapper1 = new QueryWrapper();
			queryWrapper1.eq("agent_id", agentId);
			queryWrapper1.eq("status", Constant.DeleteStateEnum.NORMAL.getCode());
			queryWrapper1.le("begin_Date",endDate);
			queryWrapper1.ge("end_date",endDate);

			return baseMapper.selectList(queryWrapper1);

		}


	public List<Holiday> getAgentHoliday(String agentId,String beginDate,String endDate){

		QueryWrapper<Holiday> queryWrapper1 = new QueryWrapper();
		queryWrapper1.eq("agent_id", agentId);

		return baseMapper.selectList(queryWrapper1);

	}



}

